﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using Business;

namespace Data
{


    public class DataArtista
    {
        public DataArtista()
        {
        }


        public bool updateArtista(Artista art)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "UPDATE Artista SET nome='" + art.getNomeArtista() + "',descricao='" + art.getDescricaoArtista() + "',link='" + art.getLinkArtista() + "',status=" + art.getStatusArtista() + " WHERE (idArtista=" + art.getIDArtista() + ")";
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return value;
        }

        public Artista getArtistaByID(int id)
        {
            Artista art = null;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from Artista where idArtista=" + id;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    String nomeArtista = "";
                    String descricao = "";
                    String linkArtista = "";

                    if(!reader.IsDBNull(1))
                        nomeArtista = Convert.ToString(reader[1]);
                    if(!reader.IsDBNull(2))
                        descricao = Convert.ToString(reader[2]);
                    if (!reader.IsDBNull(3))
                        linkArtista = Convert.ToString(reader[3]);

                    art = new Artista(nomeArtista,descricao,linkArtista);
                    art.setIDArtista(Convert.ToInt32(reader[0]));
                    art.setStatusArtista(Convert.ToInt16(reader[4]));
                }
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return art;
        }

        public Artista getArtista(String nome)
        {
            Artista art = null;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from Artista where nome='" + nome+"'";
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    art = new Artista(Convert.ToString(reader[1]), Convert.ToString(reader[2]), Convert.ToString(reader[3]));
                    art.setIDArtista(Convert.ToInt32(reader[0]));
                    art.setStatusArtista(Convert.ToInt16(reader[4]));
                }
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return art;
        }

        

        public bool setEstadoArtista(int artistaID, int estado)
        {
            bool value = false;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "UPDATE Artista SET status=" + estado + " WHERE (idArtista=" + artistaID + ")";
                int i = command.ExecuteNonQuery();
                if (i > 0) value = true;
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return value;

        }


        public ListaArtista getListaArtista(int status)
        {
            Artista art = null;
            Connect c = new Connect();
            ListaArtista la = new ListaArtista();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                //Verifica se o status é válido ou para mostrar todos
                if (status == -10)
                    command.CommandText = "Select * from Artista";
                else
                    command.CommandText = "Select * from Artista where status=" + status;

                //Cria lista de artistas
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    String nomeArtista = "";
                    String descricao = "";
                    String linkArtista = "";

                    if (!reader.IsDBNull(1))
                        nomeArtista = Convert.ToString(reader[1]);
                    if (!reader.IsDBNull(2))
                        descricao = Convert.ToString(reader[2]);
                    if (!reader.IsDBNull(3))
                        linkArtista = Convert.ToString(reader[3]);

                    art = new Artista(nomeArtista, descricao, linkArtista);
                    art.setIDArtista(Convert.ToInt32(reader[0]));
                    art.setStatusArtista(Convert.ToInt16(reader[4]));
                    la.addArtista(art);
                }

                //Fecha conexão e reader
                reader.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return la;
        }


        public int insereArtista(Artista art)
        {
            int value = -1;
            Connect c = new Connect();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();


                //Insere Artista
                command.CommandText = "INSERT INTO ARTISTA(nome,descricao,link,status) VALUES " + " ('" + art.getNomeArtista() + "','" + art.getDescricaoArtista() + "','" + art.getLinkArtista() + "',1)";
                int i = command.ExecuteNonQuery();

                //Vai buscar o ID do artista que doi adicionado
                command.CommandText = "SELECT MAX(idArtista) FROM Artista";
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                    value = Convert.ToInt16(reader[0]);
                reader.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return value;
        }

        public ListaArtista getSearchListaArtista(String nome)
        {
            Artista art = null;
            Connect c = new Connect();
            ListaArtista la = new ListaArtista();

            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();

                command.CommandText = "Select * from Artista where (nome LIKE '%" + nome + "%')";

                //Cria lista de artistas
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    String nomeArtista = "";
                    String descricao = "";
                    String linkArtista = "";

                    if (!reader.IsDBNull(1))
                        nomeArtista = Convert.ToString(reader[1]);
                    if (!reader.IsDBNull(2))
                        descricao = Convert.ToString(reader[2]);
                    if (!reader.IsDBNull(3))
                        linkArtista = Convert.ToString(reader[3]);

                    art = new Artista(nomeArtista, descricao, linkArtista);
                    art.setIDArtista(Convert.ToInt32(reader[0]));
                    art.setStatusArtista(Convert.ToInt16(reader[4]));
                    la.addArtista(art);
                }

                //Fecha conexÃ£o e reader
                reader.Close();
                conn.Close();
            }

            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return la;
        }

        public ListaArtista getArtistasEvento(int eventID)
        {
            Artista art = null;
            Connect c = new Connect();
            ListaArtista la = new ListaArtista();
            List<int> lai = new List<int>();
            try
            {
                SqlConnection conn = c.connect("");
                SqlCommand command = conn.CreateCommand();
                command.CommandText = "SELECT Artista_idArtista FROM EventoArtista WHERE Evento_idEvento=" + eventID;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    lai.Add(Convert.ToInt16(reader[0]));
                }
                reader.Close();
                foreach (int ai in lai)
                {
                    command.CommandText = "Select * from Artista where idArtista=" + ai;
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        String nomeArtista = "";
                        String descricao = "";
                        String linkArtista = "";

                        if (!reader.IsDBNull(1))
                            nomeArtista = Convert.ToString(reader[1]);
                        if (!reader.IsDBNull(2))
                            descricao = Convert.ToString(reader[2]);
                        if (!reader.IsDBNull(3))
                            linkArtista = Convert.ToString(reader[3]);

                        art = new Artista(nomeArtista, descricao, linkArtista);
                        art.setIDArtista(Convert.ToInt32(reader[0]));
                        art.setStatusArtista(Convert.ToInt16(reader[4]));
                        la.addArtista(art);
                    }
                    reader.Close();
                }
                conn.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return la;
        }

    }

}